﻿using Edge.Repository.Model.EMS.Dto;
using HYRN.Common.ConvertUtilities;
using HZH_Controls;
using Oliving.Common.SheBei;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace EMSClient.Common.Ems.EmsUtil
{
    public static class EmsModbusUtil
    {
        /// <summary>
        ///  获取发送命令（设备开关）
        /// </summary>
        /// <param name="equidCode">设备代码</param>
        /// <param name="state">0 关闭，1开启</param>
        public static byte[] GetSend_KaiGuanData(string hexString, int state)
        {
            #region MyRegion
            // 01 06 00 00 00 01 48 0A     （计算了crc校验）开报警
            // 01 06 00 00 00 00 89 CA     （计算了crc校验）关报警
            // 02 06 00 00 00 01 48 39     （计算了crc校验）开报警
            // 02 06 00 00 00 00 89 F9     （计算了crc校验）关报警
            // 03 06 00 00 00 01 49 E8     （计算了crc校验）开报警
            // 03 06 00 00 00 00 88 28     （计算了crc校验）关报警
            // 04 06 00 00 00 01 48 5F     （计算了crc校验）开报警
            // 04 06 00 00 00 00 89 9F     （计算了crc校验）关报警
            // 05 06 00 00 00 01 49 8E     （计算了crc校验）开报警
            // 05 06 00 00 00 00 88 4E     （计算了crc校验）关报警

            #endregion

            int num = Int32.Parse(hexString, System.Globalization.NumberStyles.HexNumber);
            Console.WriteLine(num);

            byte[] getData = new byte[8];

            getData[0] = (byte)num;
            getData[1] = 0x06;

            getData[2] = 0x00;
            getData[3] = 0x00;

            getData[4] = 0x00;
            getData[5] = (byte)state;

            getData[6] = 0x00;
            getData[7] = 0x00;
            getData = ModbusHelper.AddModbusCRC(getData);
            string stringdataA = BitConverter.ToString(getData, 0, getData.Length);

            return getData;
        }

         
        public static EquipRecordDto ConvertDto_qingqi(byte[] data)
        {
            #region 测试数据
            //42 01 23 01
            //00 01 00 01 00 02 00 02 00 03 00 03 00 04 00 04
            //00 05 00 05 00 06 00 06 00 07 00 07 00 08 00 08
            //00 09
            //76 45
            #endregion

            #region 验证数据
            if (data.Length != 40) { return null; }

            if (data[0] == (byte)0x48) { } //&& data[2] == (int)23
            else { return null; }
            #endregion

            #region 解析数据

            EquipRecordDto recordDto = new EquipRecordDto();

            recordDto.EquipCpde = ((int)(data[1])).ToString("X2").Trim();
            recordDto.IsBaoJing = ((int)data[3]) == 1;

            #region 温湿度

            List<EquipVal> WenDuList = new List<EquipVal>();
            List<EquipVal> ShiDuList = new List<EquipVal>();


            for (int i = 1; i <= 8; i++)
            {
                byte[] wenduData = new byte[2];
                byte[] shiduData = new byte[2];

                int myIndex = ((i - 1) * 4) + 4;

                wenduData[0] = data[myIndex];
                wenduData[1] = data[myIndex + 1];

                shiduData[0] = data[myIndex + 2];
                shiduData[1] = data[myIndex + 3];

                decimal wendu = Convert.ToDecimal(wenduData.ByteToInt().ObjToDecimal() / 10);
                decimal shidu = Convert.ToDecimal(shiduData.ByteToInt().ObjToDecimal() / 10);
                //if (wendu > 100) { wendu = 0; }
                //if (shidu > 100) { shidu = 0; }
                WenDuList.Add(new EquipVal() { Index = i, Val = wendu.ToString() });
                ShiDuList.Add(new EquipVal() { Index = i, Val = shidu.ToString() });

            }

            recordDto.ShiDuList = ShiDuList;
            recordDto.WenDuList = WenDuList;

            #endregion

            #region 氢气
            byte[] qingqiData = new byte[2];
            qingqiData[0] = data[36];
            qingqiData[1] = data[37];
            decimal qingqiVal = Convert.ToDecimal(qingqiData.ByteToInt().ObjToDecimal() / 10);

            recordDto.IsQingQi = true;
            recordDto.QingQiVal = qingqiVal.ObjToString();
            #endregion
             
            #endregion
             
            return recordDto;
        }



        public static EquipRecordDto ConvertDto_wuqingqi(byte[] data)
        {
            #region 测试数据
            // 42 01 41  = 前准  3 
            // 01        = 报警  1 
            // 00 01 00 01 00 02 00 02 00 03 00 03 00 04 00 04 00 05 00 05 00 06 00 06 00 07 00 07 00 08 00 08
            // 00 01 00 01 00 02 00 02 00 03 00 03 00 04 00 04 00 05 00 05 00 06 00 06 00 07 00 07 00 08 00 08
            //           温湿度4*8*2 = 64 
            // D2 45     =  后缀 2
            //  总计 数量  4+64+2= 70
            #endregion

            #region 验证数据
            if (data.Length != 70) { return null; }

            if (data[0] == (byte)0x48) { } //&& data[2] == (int)23
            else { return null; }
            #endregion

            #region 解析数据

            EquipRecordDto recordDto = new EquipRecordDto();

            recordDto.EquipCpde = ((int)(data[1])).ToString("X2").Trim();
            // recordDto.EquipCpde = (data[1]).ObjToString().PadLeft('0');
            // recordDto.EquipCpde = (data[1]).ObjToString();
            recordDto.IsBaoJing = ((int)data[3]) == 1;

            #region 温湿度

            List<EquipVal> WenDuList = new List<EquipVal>();
            List<EquipVal> ShiDuList = new List<EquipVal>();


            for (int i = 1; i <= 16; i++)
            {
                byte[] wenduData = new byte[2];
                byte[] shiduData = new byte[2];

                // 4 8 12 16  
                int myIndex = ((i - 1) * 4) + 4;

                wenduData[0] = data[myIndex];
                wenduData[1] = data[myIndex + 1];

                shiduData[0] = data[myIndex + 2];
                shiduData[1] = data[myIndex + 3];

                decimal wendu = Convert.ToDecimal(Convert.ToDecimal(wenduData.ByteToInt()) / 10);
                decimal shidu = Convert.ToDecimal(Convert.ToDecimal(shiduData.ByteToInt()) / 10);  // Convert.ToDecimal(shiduData.ByteToInt() / 10);
                //if (wendu > 100) { wendu = 0; }
                //if (shidu > 100) { shidu = 0; }

                WenDuList.Add(new EquipVal() { Index = i, Val = wendu.ToString() });
                ShiDuList.Add(new EquipVal() { Index = i, Val = shidu.ToString() });

            }

            recordDto.ShiDuList = ShiDuList;
            recordDto.WenDuList = WenDuList;

            #endregion


            #endregion
             
            return recordDto;
        }





    }
}
